package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.entity.ParamDuty;
import com.hzeport.bgs22emduty.entity.ParamScheduleSetting;
import com.hzeport.bgs22emduty.enums.DTypeEnum;
import com.hzeport.bgs22emduty.enums.SetDutyTypeEnum;
import com.hzeport.bgs22emduty.outbound.ParamDutyRepository;
import com.hzeport.bgs22emduty.outbound.ParamScheduleSettingRepository;
import com.hzeport.bgs22emduty.outbound.db.ParamScheduleSettingRepositoryImpl;
import com.hzeport.bgs22emduty.service.ParamScheduleSettingService;
import com.hzeport.bgs22emduty.util.GuavacacheUtils;
import com.hzeport.bgs22emduty.vo.ParamDuty.DutyTimeVO;
import com.hzeport.bgs22emduty.vo.ParamScheduleSetting.ParamScheduleSettingVO;
import com.hzeport.bgs22emduty.vo.base.PageVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ParamScheduleSettingServiceImpl extends BaseServiceImpl<ParamScheduleSettingRepositoryImpl, ParamScheduleSetting> implements ParamScheduleSettingService {

    @Resource
    private ParamDutyRepository paramDutyRepository;

    @Resource
    private ParamScheduleSettingRepository paramScheduleSettingRepository;
    @Resource
    private GuavacacheUtils guavacacheUtils;

    @Override
    public BaseResult<List<DutyTimeVO>> getDutyTime(UserInfoDetailVO userInfo) {
        List<DutyTimeVO> dutyTimeVOList = new ArrayList<>();
        List<ParamDuty> list = paramDutyRepository.list(new LambdaQueryWrapper<ParamDuty>()
                .eq(ParamDuty::getDDept, userInfo.getCustomscode())
                .orderByAsc(ParamDuty::getDType));
        for (ParamDuty paramDuty : list) {
            DutyTimeVO dutyTimeVO = new DutyTimeVO();
            dutyTimeVO.setId(paramDuty.getId());
            DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss");
            dutyTimeVO.setName(DTypeEnum.getDescByCode(paramDuty.getDType()) + "（" + paramDuty.getDStartTime().format(format) + "-" + paramDuty.getDEndTime().format(format) + ")");
            dutyTimeVOList.add(dutyTimeVO);
        }
        return BaseResult.ok(dutyTimeVOList);
    }

    @Override
    public BaseResult<Page<ParamScheduleSetting>> getPage(UserInfoDetailVO userInfo, PageVO pageVO) {
        Page<ParamScheduleSetting> page=new Page<>(pageVO.getPageNum(),pageVO.getPageSize());
        Page<ParamScheduleSetting> paramScheduleSettingPage = paramScheduleSettingRepository.page(page, new LambdaQueryWrapper<ParamScheduleSetting>()
                .eq(ParamScheduleSetting::getCustomId, userInfo.getCustomscode())
                .orderByAsc(ParamScheduleSetting::getDutyType));
        for (ParamScheduleSetting record : paramScheduleSettingPage.getRecords()) {
            record.setDutyType(SetDutyTypeEnum.getDescByCode(record.getDutyType()));
            ParamDuty paramDuty = guavacacheUtils.getParamDuty(record.getParamDutyId());
            if( !ObjectUtils.isEmpty( paramDuty ) ){
                DateTimeFormatter format = DateTimeFormatter.ofPattern("HH:mm:ss");
                record.setParamDutyId(DTypeEnum.getDescByCode(paramDuty.getDType()) + "（" + paramDuty.getDStartTime().format(format) + "-" + paramDuty.getDEndTime().format(format) + ")");
            }
        }
        return BaseResult.ok(paramScheduleSettingPage);
    }

    @Override
    public BaseResult<String> save(UserInfoDetailVO userInfo, ParamScheduleSettingVO paramScheduleSettingVO) {
        List<ParamScheduleSetting> paramScheduleSettings=new ArrayList<>();
        List<String> ids = paramScheduleSettingRepository.list(new LambdaQueryWrapper<ParamScheduleSetting>()
                .eq(ParamScheduleSetting::getCustomId, userInfo.getCustomscode())
                .eq(ParamScheduleSetting::getDutyType, paramScheduleSettingVO.getDutyType()))
                .stream().map(ParamScheduleSetting::getParamDutyId).collect(Collectors.toList());
        List<String> collect =ids.stream().filter(id -> !paramScheduleSettingVO.getParamDutyIds().contains(id)).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(collect)){
            paramScheduleSettingRepository.remove(new LambdaQueryWrapper<ParamScheduleSetting>()
                    .eq(ParamScheduleSetting::getCustomId, userInfo.getCustomscode())
                    .eq(ParamScheduleSetting::getDutyType, paramScheduleSettingVO.getDutyType())
                    .in(ParamScheduleSetting::getParamDutyId,collect));
        }
        for (String paramDutyId : paramScheduleSettingVO.getParamDutyIds()) {
            ParamScheduleSetting one = paramScheduleSettingRepository.getOne(new LambdaQueryWrapper<ParamScheduleSetting>()
                    .eq(ParamScheduleSetting::getCustomId, userInfo.getCustomscode())
                    .eq(ParamScheduleSetting::getDutyType, paramScheduleSettingVO.getDutyType())
                    .eq(ParamScheduleSetting::getParamDutyId, paramDutyId));
            if (one!=null){
                continue;
            }
            ParamScheduleSetting paramScheduleSetting=new ParamScheduleSetting();
            paramScheduleSetting.setParamDutyId(paramDutyId);
            paramScheduleSetting.setDutyType(paramScheduleSettingVO.getDutyType());
            paramScheduleSetting.setCustomId(userInfo.getCustomscode());
            paramScheduleSetting.setCustomName(userInfo.getCustomsName());
            paramScheduleSetting.setDelFlag("0");
            paramScheduleSettings.add(paramScheduleSetting);
        }
        paramScheduleSettingRepository.saveBatch(paramScheduleSettings);
        return BaseResult.ok(null,"编辑成功");
    }

    @Override
    public BaseResult<List<String>> getDutyTimeIds(UserInfoDetailVO userInfo, String code) {
        List<ParamScheduleSetting> list = paramScheduleSettingRepository.list(new LambdaQueryWrapper<ParamScheduleSetting>()
                .eq(ParamScheduleSetting::getCustomId, userInfo.getCustomscode())
                .eq(ParamScheduleSetting::getDutyType, code));
        return BaseResult.ok(list.stream().map(ParamScheduleSetting::getParamDutyId).collect(Collectors.toList()));
    }
}
